Reading Files
Lecture 5
Survey Results
Review: Conditionals
- Condition:
- <, >, <=, >=, ==, !=
if(_some condition here_) {
Code here runs
if condition is true
} else {
Code here runs if
condition is false
}
Review: Conditionals
- ifelse() used with vectors
Goodbye sleep dataset
(for now)
Reading Files
- sleep dataset was a built-in dataset
- Default downloaded when you downloaded R
- What if we wanted to work with our own data?
- Need to read file into R
How to read a file into R
1. We’ll first learn the hard way (manually import)
a. Works in both R and RStudio
1. Then we’ll learn it the easy way RStudio allows us to do
a. Only works in RStudio
Manually: File Path
- Give R an address to find your file
- If this changes (you move the file) you have to modify
your code as well
File Path
Option 1
1. Search file
name
2. Right click
3. Select “Copy
full path”
File Path Option 2
1. Go to folder where file is located
2. Click the folder icon
3. Highlighted text is path to that folder, need to add
\file_name_here
Reading Files: function
R Documentation for
read.table()
Versions also available
for
csv (comma separated)
and
tsv (tab separated) files
Reading Files: delimiters
- Delimiters separate data
Delimiter
Data
Function
Space
1
2 3 4 5
read.table(file = filepath, …)
Comma
1
,2,3,4,5
read.csv(file = filepath, …)
Tab
1
\t2\t3\t4\t5
read.delim(file = filepath, ...)
Reading Files: Example
Syntax:
read.table(file = filepath, ..other options to customize…)
Note: read.table() should fit
onto one line
Default: header = FALSE for
read.table() (but default TRUE for
read.csv() and read.delim())
Reading Files: Example Notes
Syntax:
read.table(file = filepath, ..other options to customize…)
Note: read.table() should fit
onto one line
Store table in a variable so
you can access it later!
RStudio: Import Data
- File
- Import Dataset
- From Text
RStudio: Import Data
- Can edit dataset
Name, delimiter,
etc.
- Press Enter
when done
RStudio: Import Data
- What appears in the console:
- Copy this code into the script! (remove the ‘>’)
- If you open script at a later time, you can see what was
imported
Your Turn: read_table_example.txt
- Try reading read_table_example.txt
- Print out the dataset
RStudio Only: File → Import Data → From Text
OR
Reading Files: read_table_example.txt
Tables are like matrices
Columns
Rows
Tables are like matrices
Refer to elements/rows/columns with syntax:
table_name[row_number, column_number]
Tables are like matrices
Refer to elements/rows/columns with syntax:
table_name[row_number][column_number]
What have we learned
before that does the
same thing?
Tables are like matrices
Refer to elements/rows/columns with syntax:
table_name[row_number][column_number]
Your Turn: heart.csv
- Download heart.csv
- Read heart.csv file into R
- Output a summary of age
(min, max, mean, median)
Your Turn: heart.csv
Hints:
- File → Import Data → From
Text
- summary(dataset$vector)
heart.csv
- How many males are there?
How many females?
- Males = 1, female = 0
- How many smokers are there?
- Smoker = 1, non-smoker = 0
- How many people are above the
mean age of the dataset?
Your Turn: heart.csv
Hints:
- File → Import Data → From
Text
- summary(dataset$vector)
- ifelse(conditional, if true, else
false)
- sum()
heart.csv
We just learned a lot about our study population!
Table 1 in research papers often informs us of the
study population baseline characteristics
- With a few lines of code, we are able to get these
values without counting through hundreds of data
points
Logistics
Reminder: assignment 2 due before start of next
class
Announcement will be sent out later
today/tomorrow about how lecture 6 will work